<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use Encore\Admin\Controllers\AdminController;
//use Encore\Admin\Controllers\UserController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;

class CategoryController extends AdminController
{


//    use ModelTree;


    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品分类';

    /**
     *
     */
    public function index(Content $content)
    {

        return $content->title('分类')
            ->row(function(Row $row) {
                $row->column(6, $this->treeView()->render());


                //编辑
                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_url('category'));

                    $menuModel = new Category();

                    $form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions());
                    $form->text('title', trans('admin.title'))->rules('required');
                    $form->hidden('_token')->default(csrf_token());

                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });
    }



    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        $menuModel = new Category();

        $form = new Form($menuModel);

        $form->display('id', 'ID');

        $form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions());
        $form->text('title', trans('admin.title'))->rules('required');

        //$form->display('created_at', trans('admin.created_at'));
        //$form->display('updated_at', trans('admin.updated_at'));

        return $form;
    }



    protected function treeView()
    {

        $tree = new Tree(new Category);

        $tree->branch(function ($branch) {
            $payload = "&nbsp;<strong>{$branch['title']}</strong>";


            return $payload;
        });

        return $tree;

    }

}
